package com.lemon.smartsafetynew.ui.activity.patrol;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.event.common.TypeEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolGetTaskBo;
import com.lemon.smartsafetynew.ui.fragment.patrol.SafePatrolTaskListFragment;
import com.lemon.smartsafetynew.util.common.AppSharePreferenceUtil;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.SafeCloudConstants;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;

/**
 * 任务路线首页
 * Created by Andrew on 2017/7/27.
 */

public class SafePatrolHomeActivity extends SwipeBackActivity {

    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    private PatrolViewpagerAdapter adapter;

    private List<SafePatrolTaskListFragment> mList = new ArrayList<>();

    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.work_options_main_11);
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_viewpage_tab_layout;
    }

    private void initData() {
        AppSharePreferenceUtil.getInstance().putString(SafeCloudConstants.SAFE_PATROL_CHOOSE_DATE, CommonUtil.getNowTimeStr(DateTypeEnum.DAY));

        /**
         *  任务状态。为空则查询所有
         * 	("1", "待执行"),
         * 	("2", "执行中"),
         * 	("3", "已完成"),
         * 	("4", "已超时");
         */
        SafePatrolTaskListFragment allTask = SafePatrolTaskListFragment.newInstance("");
        SafePatrolTaskListFragment hasFinishTask = SafePatrolTaskListFragment.newInstance(SafePatrolGetTaskBo.State.done.getValue());
        SafePatrolTaskListFragment notFinishTask = SafePatrolTaskListFragment.newInstance(SafePatrolGetTaskBo.State.to_do.getValue());

        mList.add(allTask);
        mList.add(hasFinishTask);
        mList.add(notFinishTask);
    }

    @Override
    protected void initViews() {
        super.initViews();

        initData();

        // ======= 该变指示栏的长度 begin =======
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 10, 10); //左右各缩进5dp
            }
        });
        // ======= 该变指示栏的长度 end =======

        adapter = new PatrolViewpagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

//        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                AppService.getBus().post(new TypeEvent(position));
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setIconEnable(menu, true);
        getMenuInflater().inflate(R.menu.menu_calendar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_calendar: {
                pickCheckDate();
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    private class PatrolViewpagerAdapter extends FragmentStatePagerAdapter {

        public PatrolViewpagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            //return SafePatrolTaskListFragment.newInstance(position);
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return AppSharePreferenceUtil.getInstance().getString(SafeCloudConstants.SAFE_PATROL_CHOOSE_DATE, "");
                case 1:
                    return getString(R.string.has_patrol);
                case 2:
                    return getString(R.string.not_patrol);
            }
            return null;
        }

    }

    /**
     * 修改日期
     */
    private void pickCheckDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat dateFormat= new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                String dateStr = dateFormat.format(calendar.getTime());
                AppSharePreferenceUtil.getInstance().putString(SafeCloudConstants.SAFE_PATROL_CHOOSE_DATE, dateStr);

                try {
                    tabLayout.getTabAt(0).setText(dateStr);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                viewPager.setCurrentItem(0);

                AppService.getBus().post(new TypeEvent(0));

            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }

}
